package com.tony.util.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

/**
 * @author 96543
 */
@Slf4j
public class HttpClientUtil {
    private static CloseableHttpClient CLIENT = null;
    private static final String BASE_URL = "https://etax.shandong.chinatax.gov.cn";
    static {
        SSLContextBuilder builder = new SSLContextBuilder();
        try {
            builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
            CLIENT = HttpClients.custom().setSSLSocketFactory(sslsf).build();
        } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
            e.printStackTrace();
        }
    }

    /*

    *//**
     * 登陆
     * @return 登陆结果true登陆成功，false登陆失败
     * @throws IOException 网络异常，加解密异常
     *//*
    private static boolean login(String aesCode, String verifyCode) throws Exception {
        final String url = BASE_URL +"/EnterpriseDzswjLoginAction.do";
        String zz = AESPlusUtil.encrypt("",aesCode,AESPlusUtil.CIPHER_ALGORITHM_ECB_PKCS7P);
        HttpPost httpPost = new HttpPost(url);
        List<NameValuePair> pairList = new ArrayList<>(20);
        pairList.add(new BasicNameValuePair("activity","login"));
        pairList.add(new BasicNameValuePair("mmqd","true"));
        pairList.add(new BasicNameValuePair("loginType","main"));
        pairList.add(new BasicNameValuePair("nsrsbh_Cert",""));
        pairList.add(new BasicNameValuePair("clientHello",""));
        pairList.add(new BasicNameValuePair("wsdlOper","proQxrzLogin"));
        pairList.add(new BasicNameValuePair("clientAuthCode",""));
        pairList.add(new BasicNameValuePair("serverRandom",""));
        pairList.add(new BasicNameValuePair("nsrsbh_SerNum",""));
        pairList.add(new BasicNameValuePair("userId",AESPlusUtil.encrypt(USER_ID,aesCode,AESPlusUtil.CIPHER_ALGORITHM_ECB_PKCS7P)));
        pairList.add(new BasicNameValuePair("czydm",CZY_DM));
        pairList.add(new BasicNameValuePair("password",AESPlusUtil.encrypt(PASSWORLD,aesCode,AESPlusUtil.CIPHER_ALGORITHM_ECB_PKCS7P)));
        pairList.add(new BasicNameValuePair("verifyCode",verifyCode));
        pairList.add(new BasicNameValuePair("zrruserId",zz));
        pairList.add(new BasicNameValuePair("zrrpassword",zz));
        pairList.add(new BasicNameValuePair("verifyZrrCode",""));
        pairList.add(new BasicNameValuePair("certPassword",""));

        httpPost.setEntity(new UrlEncodedFormEntity(pairList, StandardCharsets.UTF_8));
        HttpResponse httpResponse = CLIENT.execute(httpPost);
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        EntityUtils.consumeQuietly(httpResponse.getEntity());
        return HttpStatus.HTTP_MOVED_TEMP == statusCode;
    }

    *//**
     * 判断当前人是否合法
     * @return -1:请求失败，0：纳税人存在税务登记信息，1：正常
     * @throws IOException 网络异常
     *//*
    public static int isCompliance(String bwt) throws IOException {
        final String url = BASE_URL +"/GSJ/GSJ_WTDZDK_FWYDKFPSQ_NAVIGATE?method=dyccgc&bwt="+bwt+"&swjg_dm=13707040600";
        HttpPost httpPost = new HttpPost(url);
        HttpResponse httpResponse = CLIENT.execute(httpPost);
        if(HttpStatus.HTTP_OK == httpResponse.getStatusLine().getStatusCode()){
            String line = EntityUtils.toString(httpResponse.getEntity());
            EntityUtils.consumeQuietly(httpResponse.getEntity());
            if(line.contains(EXIST)){
                return 0;
            }
            return 1;
        }
        return -1;
    }

    *//**
     * 获取验证码图片
     * @return 验证码图片
     * @throws IOException 网络异常
     *//*
    public static File getCodeImg() throws IOException {
        File file = null;
        final String url = BASE_URL +"/enterprise/dzswjlogin/yzm_image.jsp?r="+ RandomUtil.randomNumber();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse = CLIENT.execute(httpGet);
        if(HttpStatus.HTTP_OK== httpResponse.getStatusLine().getStatusCode()){
            InputStream in = httpResponse.getEntity().getContent();
            file = new File(TMP_PATH + UUID.randomUUID().toString()+".jpg");
            Files.copy(in,file.toPath());
            in.close();
        }
        return file;
    }

    *//**
     * 获取验证码
     * @return ocr识别后的验证码字符串
     * @throws IOException 图像识别异常
     *//*
    public static String getCodeStr() throws IOException {
        String code = "";
        while (code.length() != 4){
            File file = getCodeImg();
            if(file!=null) {
                code = OCRUtil.getCodeStrApi(file);
            }
        }
        return code;
        //return OCRUtil.getCodeStr(getCodeImg());
    }

    *//**
     * 获取AESCode(加密用)
     * @return AESCode
     * @throws IOException 网络异常
     *//*
    public static String getAESCode() throws IOException {
        String line = null;
        final String url = BASE_URL +"/AESServiceAction.do?method=GetAESCode";
        HttpPost httpPost = new HttpPost(url);
        HttpResponse httpResponse = CLIENT.execute(httpPost);
        if(HttpStatus.HTTP_OK == httpResponse.getStatusLine().getStatusCode()){
            line = EntityUtils.toString(httpResponse.getEntity());
        }
        return line;
    }*/
}
